import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import { usePermissStore } from "../store/permiss";
import Home from "../views/Home.vue";

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      title: "登录",
    },
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/403",
    name: "403",
    meta: {
      title: "没有权限",
    },
    component: () => import("../views/403.vue"),
  },
  {
    path: "/",
    name: "Home",
    component: Home,
    children: [
      {
        path: "/dashboard",
        name: "dashboard",
        meta: {
          title: "系统首页",
          permiss: "1",
        },
        component: () => import("../views/Dashboard.vue"),
      },
      {
        path: "/permission",
        name: "permission",
        meta: {
          title: "权限管理",
          permiss: "13",
        },
        component: () => import("../views/Permission.vue"),
      },
      {
        path: "/user",
        name: "user",
        meta: {
          title: "个人中心",
        },
        component: () => import("../views/User.vue"),
      },
      // 会员start-------------------------------
      {
        path: "/member_list",
        name: "memberList",
        meta: {
          title: "会员列表",
        },
        component: () => import("../views/member/MemberList.vue"),
      },
      {
        path: "/member_list_add",
        name: "memberListAdd",
        meta: {
          title: "会员新增",
        },
        component: () => import("../views/member/Add.vue"),
      },
      // 会员end-------------------------------
      // 用户start-------------------------------
      {
        path: "/user_list",
        name: "userList",
        meta: {
          title: "用户列表",
        },
        component: () => import("../views/user/UserList.vue"),
      },
      {
        path: "/user_list_add",
        name: "userListAdd",
        meta: {
          title: "用户新增",
        },
        component: () => import("../views/user/Add.vue"),
      },
      // 会员end-------------------------------
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// router.beforeEach((to, from, next) => {
//   document.title = `${to.meta.title} | vue-manage-system`;
//   const role = localStorage.getItem("ms_username");
//   const permiss = usePermissStore();
//   if (!role && to.path !== "/login") {
//     next("/login");
//   } else if (to.meta.permiss && !permiss.key.includes(to.meta.permiss)) {
//     // 如果没有权限，则进入403
//     next("/403");
//   } else {
//     next();
//   }
// });

export default router;
